﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace NeuroSpeech.Imap
{
	public class ImapClient : TcpConnection
	{

		long id = 1;

		public bool? UID { get; set; }

		public T Request<T>(BaseImapRequest<T> req)
			where T:BaseImapResponse
		{
			req.Process(this);
			return req.Response;
		}

		public override void Connect(string host, int port, bool ssl)
		{
			base.Connect(host, port, ssl);

			string line = ReadLine();
		}

		public ImapResponseLogin Login(string username, string password) {

			return Request<ImapResponseLogin>(new ImapRequestLogin { Username = username, Password = password });

		}

		public ImapResponseList RetriveFolders() {
			return Request<ImapResponseList>(new ImapRequestList { });
		}

		public ImapResponseSelect SelectMailbox(string p)
		{
			return Request<ImapResponseSelect>(new ImapRequestSelect { Folder = p });
		}

		internal string NewCommandID()
		{
			id++;
			return id.ToString();
		}

		#region public object MessageInfo(int p)
		public ImapResponseMessageHeader MessageHeader(ulong p)
		{
			return Request<ImapResponseMessageHeader>(new ImapRequestMessageHeader { Id=p });
		}

		public ImapResponseMessageHeader MessageHeader(ulong? start,ulong? end)
		{
			return Request<ImapResponseMessageHeader>(new ImapRequestMessageHeader { FromId = start, ToId = end });
		}

		#endregion

		#region public object MessageBody(int p)
		public ImapResponseMessageBody MessageBody(ulong p, string part)
		{
			return Request<ImapResponseMessageBody>(new ImapRequestMessageBody { Id=p, Part = part });
		}
		#endregion


		public ImapResponseSearch Search(ImapRequestSearch query)
		{
			return Request<ImapResponseSearch>(query);
		}



		#region public object MessagePart(int p,MimePart p_2)
		public ImapResponseMessagePart MessagePart(ulong id, ImapMimePart part)
		{
			return Request<ImapResponseMessagePart>(new ImapRequestMessagePart { Id=id, Part=part });
		}
		#endregion

}
}